package com.soilflow.info.excelListenner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.soilflow.common.domain.SysArticle;
import com.soilflow.common.excle.im.InfoExcel;
import com.soilflow.common.excle.im.articleExcle;
import com.soilflow.common.utils.bean.BeanUtils;
import com.soilflow.info.service.ISysArticleService;
import com.soilflow.info.service.ISysCategoryService;

public class SysArticleListenner extends AnalysisEventListener<articleExcle> {

    public  ISysArticleService sysArticleService;

    public ISysCategoryService categoryService;

    public SysArticleListenner(){}

    public SysArticleListenner(ISysArticleService sysArticleService,ISysCategoryService categoryService){
        this.sysArticleService=sysArticleService;
        this.categoryService=categoryService;
    }

    @Override
    public void invoke(articleExcle articleExcle, AnalysisContext analysisContext) {
        if (articleExcle!=null) {
            String categoryName = articleExcle.getCategoryName();
            Long categoryId = categoryService.selectCategoryById(categoryName);
            SysArticle sysArticle = new SysArticle();
            sysArticle.setCategoryId(categoryId);
            BeanUtils.copyProperties(articleExcle,sysArticle);
            saveData(sysArticle);
        }

    }

    private void saveData(SysArticle sysArticle) {
        sysArticleService.save(sysArticle);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
